#!/usr/bin/perl

$ENV_PATH='.';

sub ReadFile
{
	local($file_name, $cmd_typ) = @_;

	open(FILE_IN,"<$file_name");
	while( $line = <FILE_IN> ) {
##print "STEP--- \n";
		chop $line;
		@val = split ',', $line;

		$fst_char = substr $line,0 ,1;
		if( $fst_char eq '#' ) {
			next;
		}
		$i = 0;
		until ( $val[$i] eq '') {

			@dpl_fld = split '=', $val[$i];

			if( $dpl_fld[0] eq "DPL_NAME" ) {
				$TA_DMRESOURCENAME = $dpl_fld[1];
			} elsif( $dpl_fld[0] eq "LDOM" ) {

				$TA_DMLACCESSPOINT = $dpl_fld[1];
#print "-------------- $TA_DMLACCESSPOINT\n";
			} elsif( $dpl_fld[0] eq "BLOCKTIME" ) {
				$TA_DMBLOCKTIME = $dpl_fld[1];
			} elsif( $dpl_fld[0] eq "RDOM" ) {
				$TA_DMRACCESSPOINTLIST = $dpl_fld[1];
			} elsif( $dpl_fld[0] eq "RNAME" ) {
				$TA_DMREMOTENAME = $dpl_fld[1];
			} elsif( $dpl_fld[0] eq "GRP" ) {
				$TA_SRVGRP = $dpl_fld[1];
			}
#print "---until st----------- $TA_DMLACCESSPOINT\n";
			$i += 1;
		}
print "---- $TA_DMRESOURCENAME\n";
		&S_DPL($TA_DMRESOURCENAME);
		if ( $cmd_typ eq 'delete' ) {
#print "DELETE DPL\n";
			&D_DPL($TA_DMRESOURCENAME);
		} else {
#print "INSERT DPL st.\n";
			&I_DPL($TA_DMRESOURCENAME);
			&A_DPL($TA_DMRESOURCENAME);
#print "INSERT DPL ed.\n";
		}
	}
	close(FILE_IN);
}

sub S_DPL
{
	local($DPL_NAME) =@_;

	$OP_S = "
export FLDTBLDIR32=\$TUXDIR/udataobj
export FIELDTBLS32=Usysfl32,tpadm

ud32 -C tpsysadm <<!
SRVCNM	.TMIB
TA_OPERATION	GET
TA_CLASS	T_DM_IMPORT
TA_DMRESOURCENAME	\@TTA_DMR\@
TA_DMRACCESSPOINTLIST	\@TA_DMRACCESSPOINTLIST\@

!
";

	$OP_S =~ s/\@TTA_DMR\@/$DPL_NAME/;
	$OP_S =~ s/\@TA_DMRACCESSPOINTLIST\@/$TA_DMRACCESSPOINTLIST/;

	open(FILE,">$tmp_sh");
	print FILE "$OP_S";
	close(FILE);

	!system("chmod a+x $tmp_sh");
	!system("$tmp_sh > $tmp_file_name");

	open(FILE,"$tmp_file_name");
	while( $line = <FILE>) {
		chop $line;
		@val = split '\t', $line;
#		printf ">$line $val[0] $val[1]<\n";

		if( $val[0] eq "TA_OCCURS" ) {
			$TA_OCCURS = $val[1];
		}
	}
#	print "TA_OCCURS : $TA_OCCURS\n";
	!system("rm $tmp_sh $tmp_file_name");
}


sub I_DPL
{
	local($DPL_NAME) =@_;

	$OP_S = "
export FLDTBLDIR32=\$TUXDIR/udataobj
export FIELDTBLS32=Usysfl32,tpadm

ud32 -C tpsysadm <<!
SRVCNM	.TMIB
TA_OPERATION	SET
\@TA_STATE\@TA_CLASS	T_DM_IMPORT
TA_DMRESOURCENAME	\@TA_DMRESOURCENAME\@
TA_DMLACCESSPOINT	\@TA_DMLACCESSPOINT\@
TA_DMRACCESSPOINTLIST	\@TA_DMRACCESSPOINTLIST\@
TA_DMREMOTENAME	\@TA_DMREMOTENAME\@
TA_DMINBUFTYPE	CARRAY
TA_DMOUTBUFTYPE	CARRAY
TA_DMFUNCTION	DPL
TA_DMAUTOPREPARE	Y
TA_DMBLOCKTIME	\@TA_DMBLOCKTIME\@

!
";

	print "-- I_DPL\n";
	$OP_S =~ s/\@TA_DMRESOURCENAME\@/$DPL_NAME/;
	$OP_S =~ s/\@TA_DMLACCESSPOINT\@/$TA_DMLACCESSPOINT/;
	$OP_S =~ s/\@TA_DMRACCESSPOINTLIST\@/$TA_DMRACCESSPOINTLIST/;
	$OP_S =~ s/\@TA_DMREMOTENAME\@/$TA_DMREMOTENAME/;
	$OP_S =~ s/\@TA_DMBLOCKTIME\@/$TA_DMBLOCKTIME/;
	if ( $TA_OCCURS <= 0 ) {
		$OP_S =~ s/\@TA_STATE\@/TA_STATE	NEW\n/;
	} else {
		$OP_S =~ s/\@TA_STATE\@//;
	}

#	print "----------------------------------\n";
#	print "-- $OP_S\n";
#	print "----------------------------------\n";

	open(FILE,">$tmp_sh");
	print FILE "$OP_S";
	close(FILE);

	!system("chmod a+x $tmp_sh");
	!system("$tmp_sh > $tmp_file_name");

	open(FILE,"$tmp_file_name");
	while( $line = <FILE>) {
		chop $line;
		@val = split '\t', $line;

		if( $val[0] eq "TA_OCCURS" ) {
			$TA_OCCURS = $val[1];
		}
	}
#	!system("rm $tmp_sh $tmp_file_name");
	close(FILE);
}

sub A_DPL
{
	local($DPL_NAME) =@_;

	$OP_S = "
export FLDTBLDIR32=\$TUXDIR/udataobj
export FIELDTBLS32=Usysfl32,tpadm

ud32 -C tpsysadm <<!
SRVCNM	.TMIB
TA_OPERATION	SET
TA_STATE	ACT
TA_CLASS	T_SVCGRP
TA_SRVGRP	\@TA_SRVGRP\@
TA_SERVICENAME	\@TA_DMRESOURCENAME\@

!
";

	print "-- A_DPL\n";
	$OP_S =~ s/\@TA_DMRESOURCENAME\@/$DPL_NAME/;
	$OP_S =~ s/\@TA_SRVGRP\@/$TA_SRVGRP/;

#	print "----------------------------------\n";
#	print "-- $OP_S\n";
#	print "----------------------------------\n";

	open(FILE,">$tmp_sh");
	print FILE "$OP_S";
	close(FILE);

	!system("chmod a+x $tmp_sh");
	!system("$tmp_sh >> $tmp_file_name");

	open(FILE,"$tmp_file_name");
	while( $line = <FILE>) {
		chop $line;
		@val = split '\t', $line;

		if( $val[0] eq "TA_OCCURS" ) {
			$TA_OCCURS = $val[1];
		}
	}
#	!system("rm $tmp_sh $tmp_file_name");
	close(FILE);
}

sub D_DPL
{
	local($DPL_NAME) =@_;

	$OP_S = "
export FLDTBLDIR32=\$TUXDIR/udataobj
export FIELDTBLS32=Usysfl32,tpadm

ud32 -C tpsysadm <<!
SRVCNM	.TMIB
TA_OPERATION	SET
\@TA_STATE\@TA_CLASS	T_DM_IMPORT
TA_DMRESOURCENAME	\@TA_DMRESOURCENAME\@
TA_DMLACCESSPOINT	\@TA_DMLACCESSPOINT\@
TA_DMRACCESSPOINTLIST	\@TA_DMRACCESSPOINTLIST\@

!
";
	if ( $TA_OCCURS <= 0 ) {
		print "-- DPL($DPL_NAME) not exists.\n";
		goto D_DPL_END;
	}

	$OP_S =~ s/\@TA_DMRESOURCENAME\@/$DPL_NAME/;
	$OP_S =~ s/\@TA_DMLACCESSPOINT\@/$TA_DMLACCESSPOINT/;
	$OP_S =~ s/\@TA_DMRACCESSPOINTLIST\@/$TA_DMRACCESSPOINTLIST/;
	$OP_S =~ s/\@TA_STATE\@/TA_STATE	INV\n/;

	open(FILE,">$tmp_sh");
	print FILE "$OP_S";
	close(FILE);

	!system("chmod a+x $tmp_sh");
	!system("$tmp_sh > $tmp_file_name");

	open(FILE,"$tmp_file_name");
	while( $line = <FILE>) {
		chop $line;
		@val = split '\t', $line;

		if( $val[0] eq "TA_OCCURS" ) {
			$TA_OCCURS = $val[1];
		}
	}
#	!system("rm $tmp_sh $tmp_file_name");
	close(FILE);
D_DPL_END:
}
sub BackUp_Ubb_pre
{
	local($TYPE) =@_;
#	print "* backup ubb. file\n";

	$node_name=`uname -n`;
	$env_path=$ENV_PATH;
	$date_time=`date -n +"%y%m%d%H%M"`;

	chomp $node_name;
	chomp $env_path;
	chomp $date_time;
	if ( $TYPE eq 'new') {
		!system("dmunloadcf > $env_path/domshm_$node_name\_$date_time");
	} else {
		!system("dmunloadcf > $env_path/domshm_$node_name");
	}

}

$file_name = @ARGV[0];
$cmd_typ = @ARGV[1];
$tmp_file_name = 'rslt';
$tmp_sh = 'sh.sh';

#&BackUp_Ubb_pre('old');

if ($file_name eq '') {
	print "\nUsage: $0 file_name\n";
	print "\n- input file format :\n";
	print "----
DPL_NAME=A114,LDOM=THDOM1601,BLOCKTIME=16,RDOM=HDOM11,RNAME=HT01:PZHTI01
#DPL_NAME=A114,LDOM=THDOM1601,BLOCKTIME=16,RDOM=HDOM11,RNAME=HT01:PZHTI01
----\n";
	goto END;
}

&ReadFile($file_name, $cmd_typ);


#&BackUp_Ubb_pre('new');
END:
